package com.opennews.openplatform.familyexpenseapi.service

import com.opennews.openplatform.familyexpenseapi.common.constant.ExpenseType
import com.opennews.openplatform.familyexpenseapi.common.constant.IncomeExpenseType
import com.opennews.openplatform.familyexpenseapi.common.constant.IncomeType
import com.opennews.openplatform.familyexpenseapi.entity.Tables.INCOME_EXPENSE_DETAIL_BASE_TYPE
import com.opennews.openplatform.familyexpenseapi.entity.tables.records.IncomeExpenseDetailBaseTypeRecord
import com.opennews.openplatform.familyexpenseapi.jooq.DSLContextWrapper
import org.jooq.DSLContext
import org.springframework.stereotype.Service

@Service
class IncomeExpenseDetailBaseTypeService(
    private val dslContext: DSLContext,
    private val dslContextWrapper: DSLContextWrapper,
) {
    /**
     * Prepares all default types.
     */
    fun init() {
        if (!dslContextWrapper.exists(INCOME_EXPENSE_DETAIL_BASE_TYPE)) {
            val records = mutableListOf<IncomeExpenseDetailBaseTypeRecord>().apply {
                add(
                    createRecord(
                        IncomeExpenseType.EXPENSE,
                        ExpenseType.EXPENSE_FAMILY,
                        "日常家庭各类开销",
                        "如超市、用餐、网购等。",
                        1,
                    )
                )
                add(
                    createRecord(
                        IncomeExpenseType.EXPENSE,
                        ExpenseType.EXPENSE_LIVING_COST,
                        "生活成本",
                        "如水电煤、通讯费、宽带等。",
                        2,
                    )
                )
                add(
                    createRecord(
                        IncomeExpenseType.EXPENSE,
                        ExpenseType.EXPENSE_EDUCATION,
                        "教育",
                        "如上课、考试等。",
                        3,
                    )
                )
                add(
                    createRecord(
                        IncomeExpenseType.EXPENSE,
                        ExpenseType.EXPENSE_TRANSPORTATION,
                        "交通",
                        "如乘车、交通卡充值、加油卡充值等。",
                        4,
                    )
                )
                add(
                    createRecord(
                        IncomeExpenseType.EXPENSE,
                        ExpenseType.EXPENSE_SOCIALIZING,
                        "社交",
                        "如各类人情、红包等。",
                        5,
                    )
                )
                add(
                    createRecord(
                        IncomeExpenseType.EXPENSE,
                        ExpenseType.EXPENSE_FAMILY_TRAVEL,
                        "家庭旅行",
                        "包括相关交通费、住宿费、签证费等。",
                        6,
                    )
                )
                add(
                    createRecord(
                        IncomeExpenseType.EXPENSE,
                        ExpenseType.EXPENSE_BUSINESS_TRAVEL,
                        "公务差旅",
                        "比如先行个人垫付的相关差旅费用。",
                        7,
                    )
                )
                add(
                    createRecord(
                        IncomeExpenseType.EXPENSE,
                        ExpenseType.EXPENSE_FAMILY_CAR,
                        "家用车",
                        "如车辆维修、保养、车险等。",
                        8,
                    )
                )
                add(
                    createRecord(
                        IncomeExpenseType.EXPENSE,
                        ExpenseType.EXPENSE_FAMILY_INSURANCE,
                        "家庭保险",
                        "如商业养老、意外、疾病类保险等。",
                        9,
                    )
                )
                add(
                    createRecord(
                        IncomeExpenseType.EXPENSE,
                        ExpenseType.EXPENSE_FAMILY_HEALTH,
                        "家庭健康",
                        "如就医、购药、健身等。",
                        10,
                    )
                )
                add(
                    createRecord(
                        IncomeExpenseType.EXPENSE,
                        ExpenseType.EXPENSE_INVESTMENT,
                        "投资",
                        "如定投、股票、房产等。",
                        11,
                    )
                )
                add(
                    createRecord(
                        IncomeExpenseType.EXPENSE,
                        ExpenseType.EXPENSE_CREDIT_CARD_REPAYMENT,
                        "信用卡还款",
                        "用于信用卡账单还款",
                        12,
                        analysisIncluded = false,
                    )
                )
                add(
                    createRecord(
                        IncomeExpenseType.EXPENSE,
                        ExpenseType.EXPENSE_PAY_FOR_ANOTHER,
                        "代付",
                        "用于先行代付的公司、他人的费用。",
                        13,
                        analysisIncluded = false,
                    )
                )
                add(
                    createRecord(
                        IncomeExpenseType.EXPENSE,
                        ExpenseType.EXPENSE_OTHER,
                        "其它",
                        "其它类费用",
                        14,
                    )
                )
                add(
                    createRecord(
                        IncomeExpenseType.INCOME,
                        IncomeType.INCOME_COMPANY_SALARY,
                        "公司薪资收入",
                        1,
                    )
                )
                add(
                    createRecord(
                        IncomeExpenseType.INCOME,
                        IncomeType.INCOME_COMPANY_BONUS,
                        "公司奖金收入",
                        2,
                    )
                )
                add(
                    createRecord(
                        IncomeExpenseType.INCOME,
                        IncomeType.INCOME_COMPANY_REIMBURSEMENT,
                        "公司报销",
                        3,
                        analysisIncluded = false,
                    )
                )
                add(
                    createRecord(
                        IncomeExpenseType.INCOME,
                        IncomeType.INCOME_COMPANY_OTHER_INCOME,
                        "公司其它收入",
                        4,
                    )
                )
                add(
                    createRecord(
                        IncomeExpenseType.INCOME,
                        IncomeType.INCOME_INVESTMENT_RETURN,
                        "投资回报",
                        5,
                    )
                )
                add(createRecord(IncomeExpenseType.INCOME, IncomeType.INCOME_REFUND, "退款", 6))
                add(
                    createRecord(
                        IncomeExpenseType.INCOME,
                        IncomeType.INCOME_PAY_FOR_ANOTHER_REPAYMENT,
                        "代付回款",
                        7,
                        analysisIncluded = false,
                    )
                )
                add(createRecord(IncomeExpenseType.INCOME, IncomeType.INCOME_OTHER, "其它收入", 8))
            }

            dslContext.batchInsert(records)
        }
    }

    private fun createRecord(
        incomeExpenseType: String,
        name: String,
        title: String?,
        subtitle: String?,
        orderIndex: Int,
        analysisIncluded: Boolean,
    ): IncomeExpenseDetailBaseTypeRecord {
        return dslContext.newRecord(INCOME_EXPENSE_DETAIL_BASE_TYPE).apply {
            this.incomeExpenseType = incomeExpenseType
            this.name = name
            this.title = title
            this.subtitle = subtitle
            this.orderIndex = orderIndex
        }
    }

    private fun createRecord(
        incomeExpenseType: String,
        name: String,
        title: String,
        subtitle: String?,
        orderIndex: Int,
    ): IncomeExpenseDetailBaseTypeRecord = createRecord(incomeExpenseType, name, title, subtitle, orderIndex, true)

    private fun createRecord(
        incomeExpenseType: String,
        name: String,
        title: String,
        orderIndex: Int,
    ): IncomeExpenseDetailBaseTypeRecord = createRecord(incomeExpenseType, name, title, null, orderIndex, true)

    private fun createRecord(
        incomeExpenseType: String,
        name: String,
        title: String,
        orderIndex: Int,
        analysisIncluded: Boolean,
    ): IncomeExpenseDetailBaseTypeRecord =
        createRecord(incomeExpenseType, name, title, null, orderIndex, analysisIncluded)
}